package com.wbs.shop.nuby.utils;

import android.app.Activity;
import android.content.Context;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;


/**
 * Created by Administrator on 2017/3/23 0023.
 */

public class GlideUtils {
    /**
     * 根据上下文和 url获取 Glide的DrawableTypeRequest
     *
     * @param context 上下文
     * @param url     图片连接
     * @param <T>     Context类型
     * @param <K>     url类型
     * @return 返回DrawableTypeRequst<K> 类型
     */
    public static <T, K> void load(T context, K url, ImageView imageView) {
        try {
            if (context instanceof Fragment) {
                RequestBuilder<?> typeRequest;
                if (url instanceof String && ((String) url).endsWith(".gif"))
                    typeRequest = Glide.with((Fragment) context).asGif().load(url);
                else {
                    typeRequest = Glide.with((Fragment) context).load(url);
                }
                typeRequest.into(imageView);
            } else if (context instanceof android.app.Fragment) {
                RequestBuilder<?> typeRequest;
                if (url instanceof String && ((String) url).endsWith(".gif"))
                    typeRequest = Glide.with((android.app.Fragment) context).asGif().load(url);
                else {
                    typeRequest = Glide.with((android.app.Fragment) context).load(url);
                }
                typeRequest.into(imageView);
            } else if (context instanceof Activity) {    //包括FragmentActivity
                RequestBuilder<?> typeRequest;
                if (url instanceof String && ((String) url).endsWith(".gif"))
                    typeRequest = Glide.with((Activity) context).asGif().load(url);
                else {
                    typeRequest = Glide.with((Activity) context).load(url);
                }
                typeRequest.into(imageView);
            } else if (context instanceof Context) {
                RequestBuilder<?> typeRequest;
                if (url instanceof String && ((String) url).endsWith(".gif"))
                    typeRequest = Glide.with((Context) context).asGif().load(url);
                else {
                    typeRequest = Glide.with((Context) context).load(url);
                }
                typeRequest.into(imageView);
            } else {
                Logger.e("content不正确");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Logger.e("Glide的Context错误");
        }
    }
}
